/*--------------------------------登录账户service---------------------------------------*/
backHomeApp.factory("infoService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            logout: function(param) {
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/logout.do",
                    data: {},
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            info: function(param) {
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/has_logined.do",
                    data: {},
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            itemAll: function(param) {
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/item/itemAll",
                    data: {},
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            myPrivilegeList:function() {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/role/getMenusOfRole.do",
                    data: {},
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            }


        };
        return _this;
    }
]);
/*--------------------------------客户service---------------------------------------*/
backHomeApp.factory("customerService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    type:"get",
                    url: "/userSystem/client/queryByParams.do",
                    data: {
                        "name": param.name,
                        "prequalification": param.prequalification,
                        "the": param.the,
                        "isPledge": param.isPledge,
                        "intoStartDate": param.intoStartDate,
                        "intoEndDate": param.intoEndDate,
                        "brandId": param.select_brand?param.select_brand.id:"",
                        "companyId": param.select_store?param.select_store.id:"",
                        "sellId": param.select_sell?param.select_sell.id:"",
                        limit:param.rows,
                        start:(param.page-1)*param.rows,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            add: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/client/add.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:null,
                            requestDate: param.request_date_param,
                            name: param.name,
                            // idcard: param.idcard,
                            phone: param.phone,
                            clientProperty: param.clientProperty,
                            intoWay: param.intoWay,
                            address: param.address,
                            situation: param.situation,
                            brandId: param.select_brand?param.select_brand.id:"",
                            companyId: param.select_store?param.select_store.id:"",
                            carId: param.select_car?param.select_car.id:"",
                            sellId: param.select_sell?param.select_sell.id:"",
                            money: param.money,
                            periods: param.periods,
                            clientRate: param.clientRate,
                            dealerRate: param.dealerRate,
                            venderRate: param.venderRate,
                            isSurcharge: param.isSurcharge?1:0,
                            surchargeMoney: param.surchargeMoney,
                            surchargePeriods: param.surchargePeriods,
                            surchargeClientRate: param.surchargeClientRate,
                            surchargeDealerRate: param.surchargeDealerRate,
                            surchargeVenderRate: param.surchargeVenderRate,
                            prequalification: param.prequalification,
                            the: param.the,
                            intoDate: param.into_date_param||null,
                            loanDate: param.loan_date_param||null,
                            creditDate: param.credit_date_param||null,
                            isPledge: param.isPledge,
                            pledgeDetail: param.pledgeDetail||null,
                            contact: param.contact||null,
                            remark: param.remark||null,
                            clientPropertyName: param.clientPropertyName,
                            intoWayName: param.intoWayName,
                            situationName: param.situationName,
                            prequalificationName: param.prequalificationName,
                            theName: param.theName,
                        })  
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            update: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/client/update.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:param.id,
                            requestDate: param.request_date_param,
                            name: param.name,
                            // idcard: param.idcard,
                            phone: param.phone,
                            clientProperty: param.clientProperty,
                            intoWay: param.intoWay,
                            address: param.address,
                            situation: param.situation,
                            brandId: param.select_brand?param.select_brand.id:"",
                            companyId: param.select_store?param.select_store.id:"",
                            carId: param.select_car?param.select_car.id:"",
                            sellId: param.select_sell?param.select_sell.id:"",
                            money: param.money,
                            periods: param.periods,
                            clientRate: param.clientRate,
                            dealerRate: param.dealerRate,
                            venderRate: param.venderRate,
                            isSurcharge: param.isSurcharge?1:0,
                            surchargeMoney: param.surchargeMoney,
                            surchargePeriods: param.surchargePeriods,
                            surchargeClientRate: param.surchargeClientRate,
                            surchargeDealerRate: param.surchargeDealerRate,
                            surchargeVenderRate: param.surchargeVenderRate,
                            prequalification: param.prequalification,
                            the: param.the,
                            intoDate: param.into_date_param||null,
                            loanDate: param.loan_date_param||null,
                            creditDate: param.credit_date_param||null,
                            isPledge: param.isPledge,
                            pledgeDetail: param.pledgeDetail||null,
                            contact: param.contact||null,
                            remark: param.remark||null,
                            createdBy:param.createdBy || "",
                            createdDate:param.createdDate || "",
                            clientPropertyName: param.clientPropertyName,
                            intoWayName: param.intoWayName,
                            situationName: param.situationName,
                            prequalificationName: param.prequalificationName,
                            theName: param.theName,
                        })
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            delete: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/client/delete.do",
                    data: {
                        id:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            getItem: function(param){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/item/getItemByType.do",
                    data: {
                        type:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            detail: function(param){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/client/getDetail.do",
                    data: {
                        clientId:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            }
        };
        return _this;
    }
]);
/*--------------------------------品牌月统计service---------------------------------------*/
backHomeApp.factory("brandStatisticsService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/client/queryReportByBrand.do",
                    data: {
                        brandId:param.select_brand?param.select_brand.id:"",
                        intoStartDate:param.intoStartDate,
                        intoEndDate:param.intoEndDate,
                        type:param.type,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            }

        };
        return _this;
    }
]);
/*--------------------------------店面月统计service---------------------------------------*/
backHomeApp.factory("storeStatisticsService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/client/queryReportByCompany.do",
                    data: {
                        companyId:param.select_store?param.select_store.id:"",
                        intoStartDate:param.intoStartDate,
                        intoEndDate:param.intoEndDate,
                        type:param.type,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            detail: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/client/queryReportByCarForCompany.do",
                    data: {
                        companyId:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },

        };
        return _this;
    }
]);
/*--------------------------------销售人员统计service---------------------------------------*/
backHomeApp.factory("salesmanStatisticsService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/client/queryReportBySellForCompany.do",
                    data: {
                        companyId:param.select_store?param.select_store.id:"",
                        intoStartDate:param.intoStartDate,
                        intoEndDate:param.intoEndDate,
                        type:param.type,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            }

        };
        return _this;
    }
]);
/*--------------------------------品牌管理service---------------------------------------*/
backHomeApp.factory("brandManageService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/brand/query.do",
                    // url: "/view/json/brandStatistics.json",
                    data: {
                        requestJson:{
                            "name": param.name,
                        },
                        limit:param.rows,
                        start:(param.page-1)*param.rows,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            add: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/brand/add.do",
                    data: {
                        requestJson:JSON.stringify({
                            "name": param.name,
                        })
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            update: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/brand/update.do",
                    data: {
                        requestJson:JSON.stringify({
                            name: param.name,
                            id: param.id,
                        })
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            delete: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/brand/delete.do",
                    data: {
                        id: param,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            allList: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/brand/getAll.do",
                    data: {},
                    success: function(datas) {
                        defered.resolve(datas);
                    },
                    status_load:true
                });
                return defered.promise;
            }
        };
        return _this;
    }
]);
/*--------------------------------车型管理service---------------------------------------*/
backHomeApp.factory("carManageService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/car/query.do",
                    data: {
                        requestJson:{
                            "name": param.name,
                            "brandId": param.select_brand?param.select_brand.id:"",
                        },
                        limit:param.rows,
                        start:(param.page-1)*param.rows,
                        sort:"brandId"
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            add: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/car/add.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:null,
                            name: param.name,
                            brandId: param.select_brand?param.select_brand.id:"",
                        })  
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            update: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/car/update.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:param.id,
                            name: param.name,
                            brandId: param.select_brand?param.select_brand.id:"",
                        })  
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            delete: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/car/delete.do",
                    data: {
                        id:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            getCarsByBrand: function(param){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/car/getListByBrand.do",
                    data: {
                        brandId:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            getListByCompany: function(param){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/car/getListByCompany.do",
                    data: {
                        companyId:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            getListByCarAndPeriods: function(carId,periods){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/companyCar/getListByCarAndPeriods.do",
                    data: {
                        carId:carId,
                        periods:periods,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            }
        };
        return _this;
    }
]);
/*--------------------------------门店管理service---------------------------------------*/
backHomeApp.factory("storeManageService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/company/query.do",
                    data: {
                        requestJson:{
                            "name": param.name,
                            "brandId": param.select_brand?param.select_brand.id:"",
                        },
                        limit:param.rows,
                        start:(param.page-1)*param.rows,
                        sort:"brandId"
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            add: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/company/add.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:null,
                            name: param.name,
                            sortName: param.sortName,
                            brandId: param.select_brand?param.select_brand.id:"",
                            adminId: param.select_user?param.select_user.id:"",
                            cityId: param.cityId,
                            address: param.address,
                            carIds: param.carIds,
                        }) 
                        
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            update: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/company/update.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:param.id,
                            name: param.name,
                            sortName: param.sortName,
                            brandId: param.select_brand?param.select_brand.id:"",
                            adminId: param.select_user?param.select_user.id:"",
                            cityId: param.cityId,
                            address: param.address,
                            carIds: param.carIds,
                        }) 
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            delete: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/company/delete.do",
                    data: {
                        id:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            detail: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/companyCar/queryByParams.do",
                    data: {
                        carId:param.id,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            setFee:function(param){
                var defered = $q.defer();
                var array = [];
                for (var i = 0; i < param.length; i++) {
                    var obj = { 
                        "id": param[i].id, 
                        "carId": param[i].carId, 
                        "name": param[i].name, 
                        "periods": param[i].periods, 
                        "clientRate": param[i].clientRate, 
                        "dealerRate": param[i].dealerRate, 
                        "venderRate": param[i].venderRate 
                    };
                    array.push(obj);
                }
                DPUtil.httpPost({
                    url: "/userSystem/companyCar/adds.do",
                    data: {
                        requestJson:JSON.stringify({"companyCarList":array}) 
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            deleteFee: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/companyCar/delete.do",
                    data: {
                        id:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            allList: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/company/getCompanyByBrand.do",
                    data: {
                        brandId:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    },
                    status_load:true
                });
                return defered.promise;
            }
        };
        return _this;
    }
]);
/*--------------------------------销售员管理service---------------------------------------*/
backHomeApp.factory("salesmanManageService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    type:"get",
                    url: "/userSystem/sell/query.do",
                    data: {
                        requestJson:{
                            "name": param.name?param.name:"",
                            "companyId": param.companyId?param.companyId:(param.select_store?param.select_store.id:""),
                        },
                        limit:param.rows,
                        start:(param.page-1)*param.rows,
                        sort:"companyId"
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            add: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/sell/add.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:null,
                            name: param.name,
                            companyId: param.select_store?param.select_store.id:"",
                            phone: param.phone,
                            status:1
                        })  
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            update: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/sell/update.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:param.id,
                            name: param.name,
                            companyId: param.select_store?param.select_store.id:"",
                            phone: param.phone,
                            status:param.status
                        })  
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            delete: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/sell/delete.do",
                    data: {
                        id:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            updateStatus: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/sell/updateStatus.do",
                    data: {
                        sellId:param.id,
                        status:param.status,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            }
        };
        return _this;
    }
]);
/*--------------------------------用户管理service---------------------------------------*/
backHomeApp.factory("userManageService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/admin/query.do",
                    data: {
                        requestJson:{
                            "name": param.name,
                            "username": param.username
                        },
                        limit:param.rows,
                        start:(param.page-1)*param.rows,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            add: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/admin/add.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:null,
                            username: param.username,
                            password: param.password,
                            name: param.name,
                            roleId: param.roleId,
                            companyIds: param.companyIds,
                            cityId: param.cityId,
                            adminId: param.select_admin?param.select_admin.id:null,
                        }) 
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            update: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/admin/update.do",
                    data: {
                        requestJson:JSON.stringify({
                            id:param.id,
                            username: param.username,
                            password: param.password,
                            name: param.name,
                            roleId: param.roleId,
                            companyIds: param.companyIds,
                            cityId: param.cityId,
                            adminId: param.select_admin?param.select_admin.id:null,
                        }) 
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            delete: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/admin/delete.do",
                    data: {
                        id:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            roleAll:function(){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/role/getListByUnder.do",
                    data: {},
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            adminAll:function(){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/admin/getAll.do",
                    data: {},
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            updatePassword: function(id,password){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/admin/updatePassword.do",
                    data: {
                        adminId:id,
                        newPassword:password,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
        };
        return _this;
    }
]);
/*--------------------------------大小项管理service---------------------------------------*/
backHomeApp.factory("itemService", ["DPUtil", "$q",
    function(DPUtil, $q) {
        var _this = {
            list: function(param) {
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/item/query.do",
                    data: {
                        requestJson:{
                            "type": param.type,
                        },
                        limit:param.rows,
                        start:(param.page-1)*param.rows,
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            add: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/item/add.do",
                    data: {
                        requestJson:JSON.stringify({
                            'id':null,
                            'type': param.type,
                            'keyword': param.keyword,
                            'value': Number(param.value),
                        })  
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            update: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/item/update.do",
                    data: {
                        requestJson:JSON.stringify({
                            'id':param.id,
                            'type': param.type,
                            'keyword': param.keyword,
                            'value': Number(param.value),
                        })  
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            delete: function(param){
                var defered = $q.defer();
                DPUtil.httpPost({
                    url: "/userSystem/item/delete.do",
                    data: {
                        id:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
            getItem: function(param){
                var defered = $q.defer();
                DPUtil.httpGet({
                    url: "/userSystem/item/getItemByType.do",
                    data: {
                        type:param
                    },
                    success: function(datas) {
                        defered.resolve(datas);
                    }
                });
                return defered.promise;
            },
        };
        return _this;
    }
]);